package com.carsite.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import org.acegisecurity.GrantedAuthority;
import org.acegisecurity.userdetails.UserDetails;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * This class is used to generate the Struts Validator Form as well as the
 * This class is used to generate Spring Validation rules
 * as well as the Hibernate mapping file.
 *
 * <p><a href="User.java.html"><i>View Source</i></a>
 *
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 *         Updated by Dan Kibler (dan@getrolling.com)
 *  Extended to implement Acegi UserDetails interface
 *      by David Carter david@carter.net
 *
 * @struts.form include-all="false" extends="BaseForm"
 * @hibernate.class table="app_user"
 */
public class User extends BaseObject implements Serializable, UserDetails {
    private static final long serialVersionUID = 3832626162173359411L;

    protected Long id;
    
    protected String username;                    // required unique
    protected String password;                    // required
    protected String confirmPassword;
    protected String passwordHint;
    
    protected String firstName;                   // required
    protected String lastName;                   
    
    
    protected String userNo;                       // required;
    
    protected String email;                       // required; 
    
    protected String phoneNumber;                 //电话
    
    protected String mobileNo;                    //手机
    
    
    protected String faxNo;                       //传真 
    
    protected String otherTelNo;                  //其他电话
   
    protected String address;                     //地址
    
    protected String postalCode;                  //邮编
    
    
    protected Integer version;
   
    
    protected boolean enabled;
    protected boolean accountExpired;
    protected boolean accountLocked;
    protected boolean credentialsExpired;
    
    
    protected Set roles = new HashSet();
    protected Branch       branch;            //所属机构
    protected Department   dept;              //所属部门
    

    public User() {}

    public User(String username) {
        this.username = username;
    }

    /**
     * @struts.form-field form-name="UserForm"
     * @hibernate.id column="id" generator-class="increment" unsaved-value="null"
     */
    public Long getId() {
        return id;
    }

    /**
     * @struts.form-field form-name="UserForm"
     * @struts.validator type="required"
     * @hibernate.property length="50" not-null="true" unique="true"
     */
    public String getUsername() {
        return username;
    }

    /**
     * @struts.form-field form-name="UserForm"
     *  
     * @struts.validator type="required"
     * @struts.validator type="twofields" msgkey="errors.twofields"
     * @struts.validator-args arg1resource="userForm.password"
     * @struts.validator-args arg1resource="userForm.confirmPassword"
     * @struts.validator-var name="secondProperty" value="confirmPassword"
     * @hibernate.property column="password" not-null="true"
     */
    public String getPassword() {
        return password;
    }

    /**
     * @struts.form-field form-name="UserForm"
     * @struts.validator type="required"
     */
    public String getConfirmPassword() {
        return confirmPassword;
    }

    /**
     * @struts.form-field form-name="UserForm"
     * @hibernate.property column="password_hint" not-null="false"
     */
    public String getPasswordHint() {
        return passwordHint;
    }

    /**
     * @struts.form-field form-name="UserForm"
     * @struts.validator type="required"
     * @hibernate.property column="first_name" not-null="true" length="50"
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @struts.form-field form-name="UserForm"
     * @hibernate.property column="last_name" not-null="false" length="50"
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @struts.form-field form-name="UserForm"
     * @struts.validator type="required"
     * @struts.validator type="email"
     * @hibernate.property name="email" not-null="false" 
     */
    public String getEmail() {
        return email;
    }

    /**
     * @struts.form-field form-name="UserForm"
     * @struts.validator type="mask" msgkey="errors.phone"
     * @struts.validator-var name="mask" value="${phone}"
     * @hibernate.property column="phone_number" not-null="false"
     */
    public String getPhoneNumber() {
        return phoneNumber;
    }
    /**
     * @struts.form-field form-name="UserForm"
     * @hibernate.property length="50" not-null="false"
     */
	public String getMobileNo() {
		return mobileNo;
	}

    /**
     * @struts.form-field form-name="UserForm"
     * @struts.validator type="required"
     * @hibernate.property length="50" 
     */
	public String getUserNo() {
		return userNo;
	}
   

    /**
     * @struts.form-field form-name="UserForm"
     * Returns the full name.
     */
    public String getFullName() {
        return firstName ;
    }
    /**
     * @struts.form-field form-name="UserForm"
     * @hibernate.property column="address" not-null="false" length="150"
     */
    public String getAddress() {
		return address;
	}

    /**
     * @struts.form-field form-name="UserForm"
     * @struts.validator type="mask" msgkey="errors.faxNo"
     * @struts.validator-var name="mask" value="${phone}"
     * @hibernate.property column="faxNo" not-null="false"
     */
    
	public String getFaxNo() {
		return faxNo;
	}
   
	 /**
	  * @struts.form-field form-name="UserForm"
     * @struts.validator type="mask" msgkey="errors.otherTelNo"
     * @struts.validator-var name="mask" value="${phone}"
     * @hibernate.property column="otherTelNo" not-null="false"
     */
	
	public String getOtherTelNo() {
		return otherTelNo;
	}
	 /**
     *@struts.form-field form-name="UserForm"
     * @struts.validator type="mask" msgkey="errors.zip"
     * @struts.validator-var name="mask" value="${zip}"
     * @hibernate.property column="postal_code" not-null="false" length="15"
     */
	public String getPostalCode() {
		return postalCode;
	}
	
   

    /**
     * @struts.form-field form-name="UserForm"
     * @hibernate.set table="user_role" cascade="save-update" lazy="false"
     * @hibernate.collection-key column="user_id"
     * @hibernate.collection-many-to-many class="com.carsite.model.Role" column="role_id"
     */
    public Set getRoles() {
        return roles;
    }

    /**
     * Adds a role for the user
     * @param role
     */
    public void addRole(Role role) {
        getRoles().add(role);
    }
    
    /**
     * @see org.acegisecurity.userdetails.UserDetails#getAuthorities()
     */
    public GrantedAuthority[] getAuthorities() {
        return (GrantedAuthority[]) roles.toArray(new GrantedAuthority[0]);
    }

    /**
     * @struts.form-field form-name="UserForm"
     * @hibernate.version
     */
    public Integer getVersion() {
        return version;
    }
    
    /**
     * @struts.form-field form-name="UserForm"
     * @hibernate.property column="account_enabled" type="yes_no"
     */
    public boolean isEnabled() {
        return enabled;
    }
    
    /**
     * @struts.form-field form-name="UserForm"
     * @hibernate.property column="account_expired" not-null="true" type="yes_no"
     */
    public boolean isAccountExpired() {
        return accountExpired;
    }
    
    /**
     * @struts.form-field form-name="UserForm"
     * @see org.acegisecurity.userdetails.UserDetails#isAccountNonExpired()
     */
    public boolean isAccountNonExpired() {
        return !isAccountExpired();
    }

    /**
     * @struts.form-field form-name="UserForm"
     * @hibernate.property column="account_locked" not-null="true" type="yes_no"
     */
    public boolean isAccountLocked() {
        return accountLocked;
    }
    
    /**
     * @struts.form-field form-name="UserForm"
     * @see org.acegisecurity.userdetails.UserDetails#isAccountNonLocked()
     */
    public boolean isAccountNonLocked() {
        return !isAccountLocked();
    }

    /**
     * @struts.form-field form-name="UserForm"
     * @hibernate.property column="credentials_expired" not-null="true"  type="yes_no"
     */
    public boolean isCredentialsExpired() {
        return credentialsExpired;
    }
    
    
    /**
     * @struts.form-field form-name="UserForm"
     * @see org.acegisecurity.userdetails.UserDetails#isCredentialsNonExpired()
     */
    public boolean isCredentialsNonExpired() {
        return !credentialsExpired;
    }
    
    
    /**
     * @hibernate.many-to-one  cascade="none"
     *  column="branchId"  
     *  inverse="true"
     */
    
    public Branch getBranch() {
		return branch;
	}
    
    /**
     * @hibernate.many-to-one  cascade="none"
     *  column="deptId"  
     *  inverse="true"
     *  lazy="false"
     */
	public Department getDept() {
		return dept;
	}
	
	
    public void setId(Long id) {
        this.id = id;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public void setPasswordHint(String passwordHint) {
        this.passwordHint = passwordHint;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

  
    public void setRoles(Set roles) {
        this.roles = roles;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }
    
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setAccountExpired(boolean accountExpired) {
        this.accountExpired = accountExpired;
    }
    
    public void setAccountLocked(boolean accountLocked) {
        this.accountLocked = accountLocked;
    }

    public void setCredentialsExpired(boolean credentialsExpired) {
        this.credentialsExpired = credentialsExpired;
    }
    
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof User)) return false;

        final User user = (User) o;

        if (username != null ? !username.equals(user.getUsername()) : user.getUsername() != null) return false;

        return true;
    }

    public int hashCode() {
        return (username != null ? username.hashCode() : 0);
    }

    public String toString() {
        ToStringBuilder sb = new ToStringBuilder(this,
                ToStringStyle.DEFAULT_STYLE).append("username", this.username)
                .append("enabled", this.enabled)
                .append("accountExpired",this.accountExpired)
                .append("credentialsExpired",this.credentialsExpired)
                .append("accountLocked",this.accountLocked);

        GrantedAuthority[] auths = this.getAuthorities();
        if (auths != null) {
            sb.append("Granted Authorities: ");

            for (int i = 0; i < auths.length; i++) {
                if (i > 0) {
                    sb.append(", ");
                }
                sb.append(auths[i].toString());
            }
        } else {
            sb.append("No Granted Authorities");
        }
        return sb.toString();
    }

	

	public void setAddress(String address) {
		this.address = address;
	}
	
	
	public void setFaxNo(String faxNo) {
		this.faxNo = faxNo;
	}
	

	public void setMobileNo(String mobileNo) {
		this.mobileNo = mobileNo;
	}

	

	public void setUserNo(String userNo) {
		this.userNo = userNo;
	}

	public void setOtherTelNo(String otherTelNo) {
		this.otherTelNo = otherTelNo;
	}
    
	

	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}

	
	public void setBranch(Branch branch) {
		this.branch = branch;
	}

	

	public void setDept(Department dept) {
		this.dept = dept;
	}
    
    
}
